package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.*;
import com.burgeon.backend.EnterpriseWeChat.rest.*;
import com.burgeon.backend.EnterpriseWeChat.rest.Evaluate.GetNewestYcValue;
import com.burgeon.backend.EnterpriseWeChat.rest.Evaluate.GetStautsYcValue;
import com.burgeon.backend.EnterpriseWeChat.rest.action.ActionRest;
import com.burgeon.backend.EnterpriseWeChat.rest.retailOut.GetRetailOutByOrderId;
import com.burgeon.backend.EnterpriseWeChat.rest.vip.*;
import com.burgeon.backend.EnterpriseWeChat.service.EWAppEmpService;
import com.burgeon.backend.EnterpriseWeChat.service.ex.UpDataException;
import com.burgeon.backend.EnterpriseWeChat.utils.TestUrlUtils;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.service.CodeService;
import com.burgeon.backend.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class EWAppEmpServiceImpl implements EWAppEmpService {
    private final static Log logger = LogFactory.getLog(EWAppEmpService.class);
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public SaleStoreData EmpSaleKanBan(String token,String phone) {
        Info info = GetInfoByToken(token);
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        ShowStoreEmp showStoreEmp = new ShowStoreEmp(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            //导购看板：动作定义
            actionRest.getData("S_BULLETINBOARD_GENERATE");
            //店铺销售看板: 动作定义：
            actionRest.getData("APP_VIPSTORE_RFIM_GENERATE");
            //看板数据
            SaleStoreData saleStoreData = showStoreEmp.getData(phone);
            return saleStoreData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public SaleData SaleKanBan(String token, String phone) {
        Info info = GetInfoByToken(token);
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        FindSalerData findSalerData = new FindSalerData(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            //导购看板：动作定义
            actionRest.getData("S_BULLETINBOARD_GENERATE");
            //看板数据
            SaleData saleData = findSalerData.getData(phone);
            return saleData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public StoreData storeKanBan(String token, String phone) {
        Info info = GetInfoByToken(token);
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        FindStoreData findStoreData = new FindStoreData(info.getUrl(),info.getAccount(),info.getPassword());
        StoreData storeData = new StoreData();
        if(null == phone){

        }
        try {
            //导购看板：动作定义
            actionRest.getData("APP_VIPSTORE_RFIM_GENERATE");
            //看板数据
            storeData = findStoreData.getData(phone);
            System.err.println(storeData);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException("店铺看板"+e.getMessage());
        }
        return storeData;
    }

    /**
     * 会员升降级信息
     * @param token
     * @param storeId
     * @return
     */
    @Override
    public ArrayList<RFIMVipCount>  FindRFIMByStoreId(String token, String storeId) {
        Info info = GetInfoByToken(token);
        FindRFIMByStoreId findRFIMByStoreId = new FindRFIMByStoreId(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            return findRFIMByStoreId.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 订单评价
     * @param token
     * @param empId
     * @return
     */
    @Override
    public ArrayList<YcValueAction> GetYcValueActionByEmpId(String token, String empId) {
        Info info = GetInfoByToken(token);
        GetYcValueActionByEmpId getYcValueActionByEmpId = new GetYcValueActionByEmpId(info.getUrl(),info.getAccount(),
                info.getPassword());
        try {
            return getYcValueActionByEmpId.getData(empId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 最新
     * @param token
     * @param empId
     * @return
     */
    @Override
    public ArrayList<YcValueAction> GetNewestYcValue(String token, String empId) {
        Info info = GetInfoByToken(token);
        GetNewestYcValue getNewestYcValue = new GetNewestYcValue(info.getUrl(),info.getAccount(),
                info.getPassword());
        try {
            return getNewestYcValue.getData(empId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 好评，差评，中评
     * @param token
     * @param empId
     * @param type
     * @return
     */
    @Override
    public ArrayList<YcValueAction> GetStatusYcValue(String token, String empId, int type) {
        Info info = GetInfoByToken(token);
        GetStautsYcValue getStautsYcValue = new GetStautsYcValue(info.getUrl(),info.getAccount(),
                info.getPassword());
        try {
            return getStautsYcValue.getData(empId,type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 订单和订单详情
     * @param token
     * @param storeId
     * @param status
     * @return
     */
    @Override
    public ArrayList<YCOrder> GetYcOrder(String token, String storeId, String status,int page) {
        System.err.println("status=================="+status);
        Info info = GetInfoByToken(token);
        GetYcOrderByStoreId getYcOrderByStoreId = new GetYcOrderByStoreId(info.getUrl(),info.getAccount(),info.getPassword());
        FindYcOrderItemByDocno findYcOrderItemByDocno = new FindYcOrderItemByDocno(info.getUrl(),info.getAccount(),
                info.getPassword());
        ArrayList<YCOrder> ycOrderList = new ArrayList<>();
        try {
            ArrayList<YCOrder> ycOrders = getYcOrderByStoreId.getData(storeId, status,page);
            if(ycOrders.size()>0){
                for (YCOrder ycOrder:ycOrders) {
                    ArrayList<YCOrderItem> ycOrderItems = new ArrayList<>();
                    ArrayList<YCOrderItem> ycOrderItem = findYcOrderItemByDocno.getData(ycOrder.getDocNo());
                    for (YCOrderItem ycOrderItem1:ycOrderItem) {
                        String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".jpg";
                        String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".png";
                        if(TestUrlUtils.testUrl(jpgUrl)){
                            System.err.println(jpgUrl);
                            ycOrderItem1.setProductUrl(jpgUrl);
                        }else if (TestUrlUtils.testUrl(pngUrl)){
                            System.err.println(pngUrl);
                            ycOrderItem1.setProductUrl(pngUrl);
                        }else {
                            ycOrderItem1.setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                        }
                        ycOrderItems.add(ycOrderItem1);
                    }
                    ycOrder.setYcOrderItem(ycOrderItems);
                    ycOrderList.add(ycOrder);
                }
                return ycOrderList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 全部订单
     * @param token
     * @param storeId
     * @return
     */
    @Override
    public ArrayList<YCOrder> GetYcOrderAll(String token, String storeId,int page) {
        Info info = GetInfoByToken(token);
        GetYcOrderAll getYcOrderAll = new GetYcOrderAll(info.getUrl(),info.getAccount(),info.getPassword());
        FindYcOrderItemByDocno findYcOrderItemByDocno = new FindYcOrderItemByDocno(info.getUrl(),info.getAccount(),
                info.getPassword());
        ArrayList<YCOrder> ycOrderList = new ArrayList<>();

        try {
            ArrayList<YCOrder> ycOrders = getYcOrderAll.getData(storeId,page);
            if(ycOrders.size()>0){
                for (YCOrder ycOrder:ycOrders) {

                    ArrayList<YCOrderItem> ycOrderItems = new ArrayList<>();
                    ArrayList<YCOrderItem> ycOrderItem = findYcOrderItemByDocno.getData(ycOrder.getDocNo());

                    for (YCOrderItem ycOrderItem1:ycOrderItem) {
                        String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".jpg";
                        String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + ycOrderItem1.getProductName()+".png";
                        if(TestUrlUtils.testUrl(jpgUrl)){
                            System.err.println(jpgUrl);
                            ycOrderItem1.setProductUrl(jpgUrl);
                        }else if (TestUrlUtils.testUrl(pngUrl)){
                            System.err.println(pngUrl);
                            ycOrderItem1.setProductUrl(pngUrl);
                        }else {
                            ycOrderItem1.setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                        }
                        ycOrderItems.add(ycOrderItem1);
                    }

                    ycOrder.setYcOrderItem(ycOrderItems);
                    ycOrderList.add(ycOrder);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ycOrderList;
    }

    /**
     * 店铺/导购的顾客总会员数
     * @param token
     * @param empId
     * @return
     */
    @Override
    public ArrayList<Vip> GetVipDataByEmpId(String token, String empId,String storeId,int page) {
        Info info = GetInfoByToken(token);
        //vip基本信息
        FindVipByEmpId findVipByEmpId = new FindVipByEmpId(info.getUrl(),info.getAccount(),info.getPassword());
        //会员行为
//        FindVipItemByVipId findVipItemByVipId = new FindVipItemByVipId(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Vip> vips = new ArrayList<>();
        try {
           vips = findVipByEmpId.getData(empId,storeId,page);
////            System.err.println(vipList);
//            for (Vip vip1:vipList) {
////                Vip vip = findVipItemByVipId.getData(vip1);
//                if("null".equals(vip.getLogoImg())){
//                    vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
//                }
////                TestUrlUtils.testUrl(vip.getLogoImg());
//
//                vips.add(vip);
//            }
            logger.info(vips);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return vips;
    }

    /**
     * 店铺或导购的每月新增会员数
     * @param token
     * @param empId
     * @param storeId
     * @param page
     * @return
     */
    @Override
    public ArrayList<Vip> GetNewVipByEmpId(String token, String empId,String storeId,int page) {
        Info info = GetInfoByToken(token);
        FindNewVipByEmpId findNewVipByEmpId = new FindNewVipByEmpId(info.getUrl(),info.getAccount(),info.getPassword());
        //会员行为
//        FindVipItemByVipId findVipItemByVipId = new FindVipItemByVipId(info.getUrl(),info.getAccount(),info.getPassword());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String creatIonDate = sdf.format(date)+"01";
        String endTime = sdf2.format(date);
        ArrayList<Vip> vipList = new ArrayList<>();

        try {
            vipList = findNewVipByEmpId.getData(empId,creatIonDate,endTime,storeId,page);
//            for (Vip vip:vips) {
//                Vip vip1 = findVipItemByVipId.getData(vip);
//                if("null".equals(vip1.getLogoImg())){
//                    vip1.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
//                }
//                vipList.add(vip1);
//            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return vipList;
    }

    /**
     * 会销额
     * @param token
     * @param empId
     * @param page
     * @return
     */
    @Override
    public ArrayList<RetailVo>  GetRetailByEmpId(String token, String empId, int page) {
        Info info = GetInfoByToken(token);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String startDate = sdf.format(date)+"01";
        String endTDate = sdf2.format(date);
        GetRateItemByEmpId getRateItemByEmpId = new GetRateItemByEmpId(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            ArrayList<RetailVo>  proNo= new ArrayList<>();
            ArrayList<Retail> retails =  getRateItemByEmpId.getData(empId,startDate,endTDate,page,token);
            /**将erp查询出来的合并，去重*/
            for (int i = 0; i < retails.size() - 1; i++) {
                ArrayList<Retail> retailList = new ArrayList<>();
                String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(i).getProductName()+".jpg";
                String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(i).getProductName()+".png";
                if(TestUrlUtils.testUrl(jpgUrl)){
                    System.err.println(jpgUrl);
                    retails.get(i).setProductUrl(jpgUrl);
                }else if (TestUrlUtils.testUrl(pngUrl)){
                    System.err.println(pngUrl);
                    retails.get(i).setProductUrl(pngUrl);
                }else {
                    retails.get(i).setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                }
                RetailVo retailVo = new RetailVo();
                retailVo.setBillDate(retails.get(i).getBillDate());
                retailVo.setRetailDocNo(retails.get(i).getRetailDocNo());
                retailVo.setStoreName(retails.get(i).getStoreName());
                retailList.add(retails.get(i));
                for (int j = retails.size() - 1; j > i; j--) {
                    /**颜色和尺寸相同并且门店不同的*/
                    if (retails.get(j).getRetailDocNo().equals(retails.get(i).getRetailDocNo())
                            && retails.get(j).getBillDate().equals(retails.get(i).getBillDate())
                    ) {

                        String jpgUrl2 = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(j).getProductName()+".jpg";
                        String pngUrl2 = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(j).getProductName()+".png";
                        if(TestUrlUtils.testUrl(jpgUrl2)){
                            System.err.println(jpgUrl2);
                            retails.get(j).setProductUrl(jpgUrl2);
                        }else if (TestUrlUtils.testUrl(pngUrl2)){
                            System.err.println(pngUrl2);
                            retails.get(j).setProductUrl(pngUrl2);
                        }else {
                            retails.get(j).setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                        }

                        retailList.add(retails.get(j));
                        retails.remove(j);
                    }
//                    else if(!retails.get(j).getRetailDocNo().equals(retails.get(i).getRetailDocNo())){
//                        retailVo.setBillDate(retails.get(j).getBillDate());
//                        retailVo.setRetailDocNo(retails.get(j).getRetailDocNo());
//                        retailVo.setStoreName(retails.get(j).getStoreName());
//                        ArrayList<Retail> retailList2 = new ArrayList<>();
//                        retailList2.add(retails.get(j));
//                        retailVo.setRetails(retailList2);
//                        proNo.add(retailVo);
//                        retails.remove(j);
//                    }

                }
                retailVo.setRetails(retailList);
                proNo.add(retailVo);
            }
            System.err.println(proNo.size());
            return proNo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 店铺会销额
     * @param token
     * @param storeId
     * @param page
     * @return
     */
    @Override
    public ArrayList<RetailVo>  GetRetailByStoreId(String token, String storeId, int page) {
        Info info = GetInfoByToken(token);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String startDate = sdf.format(date)+"01";
        String endTDate = sdf2.format(date);
        GetRateItemByStoreId getRateItemByStoreId = new GetRateItemByStoreId(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            ArrayList<Retail> retails = getRateItemByStoreId.getData(storeId,startDate,endTDate,page,token);
            ArrayList<RetailVo>  proNo= new ArrayList<>();

            /**将erp查询出来的合并，去重*/
            for (int i = 0; i < retails.size() - 1; i++) {
                ArrayList<Retail> retailList = new ArrayList<>();
//                String jpgUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(i).getProductName()+".jpg";
//                String pngUrl = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(i).getProductName()+".png";
//                if(TestUrlUtils.testUrl(pngUrl)){
//                    System.err.println(jpgUrl);
//                    retails.get(i).setProductUrl(jpgUrl);
//                }else if (TestUrlUtils.testUrl(pngUrl)){
//                    System.err.println(pngUrl);
//                    retails.get(i).setProductUrl(pngUrl);
//                }else {
//                    retails.get(i).setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
//                }
                RetailVo retailVo = new RetailVo();
                retailVo.setBillDate(retails.get(i).getBillDate());
                retailVo.setRetailDocNo(retails.get(i).getRetailDocNo());
                retailVo.setStoreName(retails.get(i).getStoreName());
                retailList.add(retails.get(i));
                for (int j = retails.size() - 1; j > i; j--) {
                    /**颜色和尺寸相同并且门店不同的*/
                    if (retails.get(j).getRetailDocNo().equals(retails.get(i).getRetailDocNo())
                            && retails.get(j).getBillDate().equals(retails.get(i).getBillDate())
                    ) {
                        String jpgUrl2 = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(j).getProductName()+".jpg";
                        String pngUrl2 = "http://"+info.getUrl() + "/html/M_PRODUCT/" + retails.get(j).getProductName()+".png";
                        if(TestUrlUtils.testUrl(jpgUrl2)){
                            System.err.println(jpgUrl2);
                            retails.get(j).setProductUrl(jpgUrl2);
                        }else if (TestUrlUtils.testUrl(pngUrl2)){
                            System.err.println(pngUrl2);
                            retails.get(j).setProductUrl(pngUrl2);
                        }else {
                            retails.get(j).setProductUrl("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/product.png");
                        }
                        retailList.add(retails.get(j));
                        retails.remove(j);
                    }
//                    else if(!retails.get(j).getRetailDocNo().equals(retails.get(i).getRetailDocNo())){
//                        retailVo.setBillDate(retails.get(j).getBillDate());
//                        retailVo.setRetailDocNo(retails.get(j).getRetailDocNo());
//                        retailVo.setStoreName(retails.get(j).getStoreName());
//                        ArrayList<Retail> retailList2 = new ArrayList<>();
//                        retailList2.add(retails.get(j));
//                        retailVo.setRetails(retailList2);
//                        proNo.add(retailVo);
//                        retails.remove(j);
//                    }

                }
                retailVo.setRetails(retailList);
                proNo.add(retailVo);
            }
            System.err.println(proNo.size());
            return proNo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 储值金额
     * @param token
     * @param empId
     * @param storeId
     * @param page
     * @return
     */
    @Override
    public ArrayList<VipDeposit> GetVipDeposit(String token, String empId, String storeId, int page) {
        Info info = GetInfoByToken(token);
        GetVipDeposit getVipDeposit = new GetVipDeposit(info.getUrl(),info.getAccount(),info.getPassword());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String startDate = sdf.format(date)+"01";
        String endTDate = sdf2.format(date);
        try {
            return getVipDeposit.getData(empId,storeId,startDate,endTDate,page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 会员预警
     * @param warningType
     * @param storeId
     * @return
     */
    @Override
    public ArrayList<Vip> queryVipWarning(String token,String warningType, String storeId,int page) {
        Info info = GetInfoByToken(token);
        queryVipWarning queryVipWarning = new queryVipWarning(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Vip> vipArrayList = new ArrayList<>();
        try {
            ArrayList<Vip> vips = queryVipWarning.getData(warningType, storeId,page);
            for (Vip vip:vips) {
                if("null".equals(vip.getLogoImg())){
                    vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
                }
                vipArrayList.add(vip);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vipArrayList;
    }

    /**
     * 搜多会员
     * @param token
     * @param cardNo
     * @return
     */
    @Override
    public Vip FindVipByCardNo(String token, String cardNo,String empId,String storeId,String type) {
        Info info = GetInfoByToken(token);
        FindVipByCardNo findVipByCardNo = new FindVipByCardNo(info.getUrl(),info.getAccount(),info.getPassword());
        FindVipNewstByCardNo findVipNewstByCardNo = new FindVipNewstByCardNo(info.getUrl(),info.getAccount(),info.getPassword());
//        Vip vip = new Vip();
        try {
            Vip vip = null;
            if("1".equals(type)){//1是全部
                 vip = findVipByCardNo.getData(cardNo,empId,storeId);
            }else if ("2".equals(type)){//2是最新
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
                Date date = new Date();
                String creatIonDate = sdf.format(date)+"01";
                String endTime = sdf2.format(date);
                vip = findVipNewstByCardNo.getData(cardNo,empId,storeId,creatIonDate,endTime);
            }
            if(vip != null){
                if ("null".equals(vip.getLogoImg())) {
                    vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
                }
                return vip;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }


}
